package com.woniuxy.purchase.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.purchase.entity.dto.DemandDetailsDTO;
import com.woniuxy.purchase.entity.dto.SelectDemandDTO;
import com.woniuxy.purchase.entity.pojo.BuyDemand;
import com.woniuxy.purchase.entity.pojo.BuyPurchase;
import com.woniuxy.purchase.entity.pojo.DemandGoods;
import com.woniuxy.purchase.entity.pojo.PurchaseGoods;
import com.woniuxy.purchase.entity.vo.DemandDetailsVO;
import com.woniuxy.purchase.entity.vo.TopDemandVO;
import com.woniuxy.purchase.mapper.BuyDemandMapper;
import com.woniuxy.purchase.mapper.BuyPurchaseMapper;
import com.woniuxy.purchase.mapper.DemandGoodsMapper;
import com.woniuxy.purchase.mapper.PurchaseGoodsMapper;
import com.woniuxy.purchase.service.BuyDemandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BuyDemandServiceImpl implements BuyDemandService {
    @Resource
    private HotGoodsThread hotGoodsThread;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Resource
    private BuyDemandMapper buyDemandMapper;
    @Resource
    private DemandGoodsMapper demandGoodsMapper;
    @Resource
    private BuyPurchaseMapper buyPurchaseMapper;
    @Resource
    private PurchaseGoodsMapper purchaseGoodsMapper;

    @Override
    public Page<BuyDemand> findDemandByCondition(SelectDemandDTO o) {
        //1.创建分页对象
        Page<BuyDemand> page = new Page<>(o.getCurrent(), o.getPageSize());
        //2.设置查询条件
        LambdaQueryWrapper<BuyDemand> lqw = new LambdaQueryWrapper<>();
        lqw.eq(o.getState()!=null,BuyDemand::getState,o.getState());
        //3.将查询条件和分页参数赋给page
        return buyDemandMapper.selectPage(page, lqw);
    }

    @Override
    public List<DemandDetailsVO> findDemandDetails(Integer id) {
        return buyDemandMapper.findDemandDetails(id);
    }



    @Override
    public void insertDemand(DemandDetailsDTO dto){
        //1.新建需求单
        BuyDemand buyDemand = new BuyDemand();
        buyDemandMapper.insert(buyDemand);
        //2.获取需求单ID
        Integer id = buyDemand.getId();
        //3.为需求单添加商品
        List<DemandGoods> list = dto.getGoodsList();
        //异步调用添加热门商品
        for (DemandGoods demandGoods : list) {
            demandGoods.setDid(id);
            demandGoodsMapper.insertDemandGoods(demandGoods);
        }
        hotGoodsThread.addHotGoods(list);
    }

    @Override
    public void demandToPurchase(Integer id) {
        //1.创建采购单并把需求表主键赋给采购表唯一字段
        BuyPurchase purchase = BuyPurchase.builder()
                .demandId(id)
                .build();
        buyPurchaseMapper.insert(purchase);
        System.out.println("id" + id);
        //2.拿到id
        Integer pid = purchase.getId();
        //3.通过demandId去获取需求表-商品中间表的信息
        QueryWrapper<DemandGoods> qw = new QueryWrapper<>();
        qw.eq("did",id);
        List<DemandGoods> demandGoods = demandGoodsMapper.selectList(qw);
        //4.把需求-商品中间表的list赋值给采购-商品中间表
        List<PurchaseGoods> purchaseGoods = new ArrayList<>();
        for (DemandGoods demandGood : demandGoods) {
            purchaseGoods.add(BeanUtil.copyProperties(demandGood, PurchaseGoods.class));
        }
        //把采购表id赋值给每一个对象
        purchaseGoods.stream().map(item->{item.setPid(pid);return purchaseGoods;}).collect(Collectors.toList());
        log.info("purchaseGoods,{}",purchaseGoods);
        //5.向数据库添加数据
        for (PurchaseGoods purchaseGood : purchaseGoods) {
            purchaseGoodsMapper.insert(purchaseGood);
        }

        //6.最后修改需求单状态为1
        BuyDemand buyDemand = new BuyDemand();
        buyDemand.setId(id);
        buyDemand.setState(1);
        buyDemandMapper.updateById(buyDemand);
    }

    @Override
    public Integer updateDemandStateById(Integer state, Integer id) {
        BuyDemand buyDemand = new BuyDemand();
        buyDemand.setId(id);
        buyDemand.setState(state);
        return buyDemandMapper.updateById(buyDemand);

    }

    @Override
    public Integer findPhoneByAccount(String phoneNumber, String account) {
        return buyDemandMapper.findPhoneByAccount(phoneNumber,account);
    }

    @Override
    public void updatePwdByAccount(String newPwd, String account) {
        newPwd = BCrypt.hashpw(newPwd);
        buyDemandMapper.updatePwdByAccount(newPwd,account);
    }

    @Override
    public List<TopDemandVO> getTopDemand() {
        return buyDemandMapper.getTopDemand();
    }


}
